package presentacion.personal;

import clases.personal.Anticipo;
import javax.swing.table.DefaultTableModel;


public final class frmConsultaAnticipo extends javax.swing.JInternalFrame {
    private int pagina;
    private String sql;
    public final Anticipo objAnticipo;
    private DefaultTableModel modelo;
    public static dlgAnticipo anticipo;

    /**
     * Creates new form frmConsultaConcepto
     */
    public frmConsultaAnticipo() {
        initComponents();
        this.setResizable(false);
        objAnticipo=new Anticipo();
        anticipo= new dlgAnticipo(this, true);
        pagina=1; 
        sql=this.objAnticipo.SqlGeneral();
        this.generartabla();
        this.inicializar();
        
    }
    
        
    public void llenarDialogo(int fila){
     this.objAnticipo.llenardatosAnticipo(modelo.getValueAt(fila, 0).toString(), modelo.getValueAt(fila, 4).toString());
     anticipo.visiblidad(false);   
     anticipo.llenardatos(); 
     this.mostrarAnticipo();
   
    }
    
    public void mostrarAnticipo(){
     anticipo.setVisible(true);
     anticipo.visiblidad(true);
    
    }
    
    public void generartabla(){
    modelo = new DefaultTableModel() {
       @Override
       public boolean isCellEditable(int fila, int columna) {
          return false; //Con esto conseguimos que la tabla no se pueda editar
       }
    };
     tblanticipos.removeAll();
     tblanticipos.setModel(modelo);
     armarColumna();
     objAnticipo.ConsultaAnticipo(modelo, tblanticipos, sql, lbltotalpaginas, lblpagina, pagina);
     
    }
    
    private void armarColumna(){
      modelo.addColumn("Cedula");
      modelo.addColumn("Apellidos");
      modelo.addColumn("Nombres");
      modelo.addColumn("Cargo");
      modelo.addColumn("Fecha");
      modelo.addColumn("Monto");
      //DefaultTableCellRenderer centrar=new DefaultTableCellRenderer();
      //centrar.setHorizontalAlignment(SwingConstants.RIGHT);
      //tblanticipos.getColumnModel().getColumn(1).setCellRenderer(centrar);
     /* TableColumn  columna= new TableColumn();
      columna=this.tblanticipos.getColumnModel().getColumn(0);
      columna.setPreferredWidth(300);
      columna=this.tblanticipos.getColumnModel().getColumn(1);
      columna.setPreferredWidth(60);*/
   }
    
    
    private void inicializar(){
    
     this.txtmonto.setVisible(false);
     this.txtmonto.setText("");
     this.dtfecha.setDate(global.Util.StringaDate(global.Util.getFechaActual()));
     this.dtfecha.setVisible(false);
     this.txtapellido.setVisible(false);
     this.txtnombre.setVisible(false);
     this.cbocargo.setVisible(false);
     this.cbocargo.setSelectedIndex(0);
     this.txtapellido.setText("");
     this.txtnombre.setText("");
     }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblanticipos = new javax.swing.JTable();
        jToolBar1 = new javax.swing.JToolBar();
        btnprimero = new javax.swing.JButton();
        btnatras = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        jLabel4 = new javax.swing.JLabel();
        lblpagina = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        lbltotalpaginas = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        btnsiguiente = new javax.swing.JButton();
        btnultimo = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        cbocriterio = new javax.swing.JComboBox();
        txtmonto = new javax.swing.JTextField();
        dtfecha = new com.toedter.calendar.JDateChooser();
        cbocargo = new javax.swing.JComboBox();
        txtnombre = new javax.swing.JTextField();
        txtapellido = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        tblanticipos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblanticipos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblanticiposMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblanticipos);

        jToolBar1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        btnprimero.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/primer.png"))); // NOI18N
        btnprimero.setFocusable(false);
        btnprimero.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnprimero.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnprimero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnprimeroActionPerformed(evt);
            }
        });
        jToolBar1.add(btnprimero);

        btnatras.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/atras.png"))); // NOI18N
        btnatras.setToolTipText("");
        btnatras.setFocusable(false);
        btnatras.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnatras.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnatras.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnatrasActionPerformed(evt);
            }
        });
        jToolBar1.add(btnatras);
        jToolBar1.add(jSeparator1);

        jLabel4.setText("Pagina ");
        jToolBar1.add(jLabel4);

        lblpagina.setText("jLabel2");
        jToolBar1.add(lblpagina);

        jLabel5.setText(" de ");
        jToolBar1.add(jLabel5);

        lbltotalpaginas.setText("jLabel1");
        jToolBar1.add(lbltotalpaginas);
        jToolBar1.add(jSeparator2);

        btnsiguiente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/siguiente.png"))); // NOI18N
        btnsiguiente.setFocusable(false);
        btnsiguiente.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnsiguiente.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnsiguiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsiguienteActionPerformed(evt);
            }
        });
        jToolBar1.add(btnsiguiente);

        btnultimo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/ultimos.png"))); // NOI18N
        btnultimo.setFocusable(false);
        btnultimo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnultimo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnultimo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnultimoActionPerformed(evt);
            }
        });
        jToolBar1.add(btnultimo);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Busqueda por:"));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel3.setText("Criterio");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 30, -1, -1));

        cbocriterio.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Todo", "Apellido", "Nombre", "Cargo", "Fecha", "Monto" }));
        cbocriterio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbocriterioActionPerformed(evt);
            }
        });
        jPanel1.add(cbocriterio, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 50, 90, -1));

        txtmonto.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtmontoKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtmontoKeyTyped(evt);
            }
        });
        jPanel1.add(txtmonto, new org.netbeans.lib.awtextra.AbsoluteConstraints(148, 50, 90, -1));

        dtfecha.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                dtfechaPropertyChange(evt);
            }
        });
        jPanel1.add(dtfecha, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 50, 120, -1));

        cbocargo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Seleccione--", "Servicio", "Docente" }));
        cbocargo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbocargoActionPerformed(evt);
            }
        });
        jPanel1.add(cbocargo, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 50, -1, -1));

        txtnombre.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtnombreKeyReleased(evt);
            }
        });
        jPanel1.add(txtnombre, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 50, 260, -1));

        txtapellido.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtapellidoKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtapellidoKeyTyped(evt);
            }
        });
        jPanel1.add(txtapellido, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 50, 260, -1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnsiguienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsiguienteActionPerformed
       if(this.objAnticipo.objPaginacion.getTotalpaginas()>pagina){
            pagina++;
            this.generartabla();
        }
    }//GEN-LAST:event_btnsiguienteActionPerformed

    private void btnultimoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnultimoActionPerformed
        pagina=this.objAnticipo.objPaginacion.getTotalpaginas();
        this.generartabla();
    }//GEN-LAST:event_btnultimoActionPerformed

    private void btnatrasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnatrasActionPerformed
        if(pagina > 1){
            pagina--;
            this.generartabla();
        }
    }//GEN-LAST:event_btnatrasActionPerformed

    private void btnprimeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnprimeroActionPerformed
         pagina=1;
        this.generartabla();
    }//GEN-LAST:event_btnprimeroActionPerformed

    private void cbocriterioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbocriterioActionPerformed
        this.inicializar();
        if(this.cbocriterio.getSelectedItem()=="Todo"){
            pagina=1;
            sql=this.objAnticipo.SqlGeneral();
            this.generartabla();

        }else if(this.cbocriterio.getSelectedItem()=="Monto"){
            this.txtmonto.setVisible(true);
            this.txtmonto.requestFocus();

        }else if(this.cbocriterio.getSelectedItem()=="Fecha"){
            this.dtfecha.setVisible(true);
            sql= this.objAnticipo.SqlFecha(global.Util.DateaString(this.dtfecha.getDate()));
            this.generartabla();
            this.dtfecha.requestFocus();

        }else if(this.cbocriterio.getSelectedItem()=="Apellido"){
            this.txtapellido.setVisible(true);
            this.txtapellido.requestFocus();
        }else if(this.cbocriterio.getSelectedItem()=="Nombre"){
            this.txtnombre.setVisible(true);
            this.txtnombre.requestFocus();
        }else if(this.cbocriterio.getSelectedItem()=="Cargo"){
            this.cbocargo.setVisible(true);
            this.cbocargo.requestFocus();
        } 
        
    }//GEN-LAST:event_cbocriterioActionPerformed

    private void txtmontoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtmontoKeyReleased
        if((global.Validaciones.comprobarNulo(txtmonto))){
            pagina=1;
            sql= this.objAnticipo.SqlMonto(Integer.parseInt(this.txtmonto.getText()));
            this.generartabla();
        }else sql=this.objAnticipo.SqlGeneral();
        this.generartabla();
    }//GEN-LAST:event_txtmontoKeyReleased

    private void txtmontoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtmontoKeyTyped
        global.Validaciones.soloNumeros(evt, txtmonto, 15);
    }//GEN-LAST:event_txtmontoKeyTyped

    private void tblanticiposMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblanticiposMouseClicked
       int click=evt.getClickCount();
       if(click==2){
       this.llenarDialogo(this.tblanticipos.getSelectedRow());
      
       }
    }//GEN-LAST:event_tblanticiposMouseClicked

    private void dtfechaPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_dtfechaPropertyChange
       if(this.cbocriterio.getSelectedItem()=="Fecha"){
            sql= this.objAnticipo.SqlFecha(global.Util.DateaString(this.dtfecha.getDate()));
            this.generartabla();
        }
    }//GEN-LAST:event_dtfechaPropertyChange

    private void cbocargoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbocargoActionPerformed
        if(cbocargo.getSelectedIndex()!=0){
            sql=this.objAnticipo.SqlCargoAnticipo(this.cbocargo.getSelectedItem().toString());
            this.generartabla();
        }else{
            sql=this.objAnticipo.SqlGeneral();
            this.generartabla();
        }
    }//GEN-LAST:event_cbocargoActionPerformed

    private void txtnombreKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtnombreKeyReleased
        global.Validaciones.convertirMayuscula(txtnombre);
        if((global.Validaciones.comprobarNulo(txtnombre))){
            pagina=1;
            sql= this.objAnticipo.SqlNombreAnticipo(this.txtnombre.getText());
            this.generartabla();
        }else sql=this.objAnticipo.SqlGeneral();
        this.generartabla();
    }//GEN-LAST:event_txtnombreKeyReleased

    private void txtapellidoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtapellidoKeyReleased
        global.Validaciones.convertirMayuscula(txtapellido);
        if((global.Validaciones.comprobarNulo(txtapellido))){
            pagina=1;
            sql= this.objAnticipo.SqlApellidoAnticipo(this.txtapellido.getText());
            this.generartabla();
        }else sql=this.objAnticipo.SqlGeneral();
        this.generartabla();
    }//GEN-LAST:event_txtapellidoKeyReleased

    private void txtapellidoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtapellidoKeyTyped
        global.Validaciones.soloLetras(evt, txtapellido);
    }//GEN-LAST:event_txtapellidoKeyTyped

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frmConsultaAnticipo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmConsultaAnticipo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmConsultaAnticipo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmConsultaAnticipo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new frmConsultaAnticipo().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnatras;
    private javax.swing.JButton btnprimero;
    private javax.swing.JButton btnsiguiente;
    private javax.swing.JButton btnultimo;
    private javax.swing.JComboBox cbocargo;
    private javax.swing.JComboBox cbocriterio;
    private com.toedter.calendar.JDateChooser dtfecha;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JLabel lblpagina;
    private javax.swing.JLabel lbltotalpaginas;
    public javax.swing.JTable tblanticipos;
    private javax.swing.JTextField txtapellido;
    private javax.swing.JTextField txtmonto;
    private javax.swing.JTextField txtnombre;
    // End of variables declaration//GEN-END:variables
}
